package com.pasabahceambalaj.session;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

import com.pasabahceambalaj.entity.Firma;
import com.pasabahceambalaj.entity.Kullanici;

@Name("kullaniciHome")
public class KullaniciHome extends EntityHome<Kullanici> {

	@In(create = true)
	FirmaHome firmaHome;

	public void setKullaniciId(Long id) {
		setId(id);
	}

	public Long getKullaniciId() {
		return (Long) getId();
	}

	@Override
	protected Kullanici createInstance() {
		Kullanici kullanici = new Kullanici();
		return kullanici;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();

		Firma firma = null;
		if (Authenticator.isAdmin()) {
			firma = firmaHome.getDefinedInstance();
		} else {
			firma = Authenticator.getFirma();
		}

		if (firma != null) {
			getInstance().setFirma(firma);
		}
	}

	public boolean isWired() {
		if (getInstance().getFirma() == null)
			return false;
		return true;
	}

	public Kullanici getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	/*
	 * protected List<Rol> getRolList() { // List<Company> resultList = //
	 * getEntityManager().createQuery(
	 * "select company from Company company left join fetch company.authorizedPerson where company.id = :id"
	 * ).setParameter("id", // getId()).getResultList();
	 * 
	 * String EJBQL = "select rol from Rol rol "; String EJBQL_no_data =
	 * "select rol from Rol rol where rol.id=-1";
	 * 
	 * if (Authenticator.isLoggedIn()) { if (!Authenticator.isAdmin()) { EJBQL =
	 * EJBQL + " where rol.firma.id = " + firma.getId(); } } else { EJBQL =
	 * EJBQL_no_data; }
	 * 
	 * List<Rol> resultList =
	 * getEntityManager().createQuery(EJBQL).getResultList();
	 * 
	 * return resultList; }
	 */
}
